@file:Suppress("unused", "MemberVisibilityCanBePrivate")

package com.csw.android.dev_utils.result

import android.app.Activity
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.csw.android.dev_utils.ui.CommonActivity

/**
 * 通过扩展函数，给activity fragment实现跳转Activity获取返回结果的函数，
 * 由于功能通过Fragment封装，所以activity必须是FragmentActivity
 *
 * 目前官方开始推荐使用registerForActivityResult，但该方式必须在Activity onStart之前先注册。
 * 等以后官方完善后再废弃这个库吧
 */
object ResultEx {

    /**
     * 跳转activity并获取请求结果
     */
    fun <T : Activity> FragmentActivity.openActivityForResult(
        activityClazz: Class<T>,
        data: Bundle? = null,
        intentSetting: ((Intent) -> Unit)? = null,
        callback: (ActivityRequestTask) -> Unit
    ) = openActivityForResult(
        data,
        {
            it.component = ComponentName(this, activityClazz)
            intentSetting?.invoke(it)
        },
        callback,
    )

    /**
     * 跳转activity并获取请求结果，这个可用于隐式意图跳转
     * @param data 跳转传递参数
     * @param intentSetting 意图设置，如action,category等等
     * @param callback 回调对象
     */
    fun FragmentActivity.openActivityForResult(
        data: Bundle? = null,
        intentSetting: ((Intent) -> Unit)? = null,
        callback: (ActivityRequestTask) -> Unit
    ) = openActivityForResult(
        ActivityRequestTask(
            Intent().apply { intentSetting?.invoke(this) },
            data,
            object : Callback {
                override fun onTaskResult(activityRequestTask: ActivityRequestTask) {
                    callback(activityRequestTask)
                }
            }
        )
    )

    /**
     * 跳转activity并获取请求结果
     */
    fun FragmentActivity.openActivityForResult(activityRequestTask: ActivityRequestTask) {
        try {
            OnActivityResultHelper.startActivityForResult(
                supportFragmentManager,
                activityRequestTask
            )
        } catch (e: Exception) {
            e.printStackTrace()
            //activity可能未找到或其他异常
            activityRequestTask.onActivityResult(null, null)
        }
    }


    /**
     * 跳转通用activity打开fragment，并取得返回结果
     */
    fun <T : Fragment> FragmentActivity.openInCommonActivityForResult(
        clazz: Class<T>,
        data: Bundle? = null,
        callback: (ActivityRequestTask) -> Unit
    ) = openActivityForResult(
        (data ?: Bundle()).apply {
            putSerializable(CommonActivity.KEY_IN_FRAGMENT_CLASS, clazz)
        },
        {
            it.component = ComponentName(this, CommonActivity::class.java)
        },
        callback
    )

    /**
     * 跳转activity并获取请求结果
     */
    fun <T : Activity> Fragment.openActivityForResult(
        activityClazz: Class<T>,
        data: Bundle? = null,
        intentSetting: ((Intent) -> Unit)? = null,
        callback: (ActivityRequestTask) -> Unit
    ) = activity?.openActivityForResult(activityClazz, data, intentSetting, callback)

    /**
     * 跳转activity并获取请求结果，这个可用于隐式意图跳转
     * @param data 跳转传递参数
     * @param intentSetting 意图设置，如action,category等等
     * @param callback 回调对象
     */
    fun Fragment.openActivityForResult(
        data: Bundle? = null,
        intentSetting: ((Intent) -> Unit)? = null,
        callback: (ActivityRequestTask) -> Unit
    ) = activity?.openActivityForResult(data, intentSetting, callback)

    /**
     * 跳转activity并获取请求结果
     */
    fun Fragment.openActivityForResult(
        activityRequestTask: ActivityRequestTask
    ) = activity?.openActivityForResult(activityRequestTask)

    /**
     * 跳转通用activity打开fragment，并取得返回结果
     */
    fun <T : Fragment> Fragment.openInCommonActivityForResult(
        clazz: Class<T>,
        data: Bundle? = null,
        callback: (ActivityRequestTask) -> Unit
    ) = activity?.openInCommonActivityForResult(clazz, data, callback)

}